package com.sin.android.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.content.Context;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver;

/**
 * This is a Utility Class
 * @author RobinTang
 * @time 2012-09-24
 */
public class SinUtil {
	
	/*
	 * The default size of the buffers
	 */
	public static int BUFFER_SIZE = 1024;
	

	/**
	 * read a String from a asset file, this file must been encoded with UTF-8
	 * @param context Context of application
	 * @param filename the name of the asset file
	 * @return if success return the text of the asset file, else return null
	 * @time 2012-09-24 
	 */
	public static String readAssetTextFile(Context context, String filename){
		try {
			InputStreamReader is = new InputStreamReader(context.getAssets().open(filename));
			BufferedReader br = new BufferedReader(is);
			int length = 0;
			char []chars = new char[BUFFER_SIZE];
			
			StringBuilder sb = new StringBuilder();
			while ((length = br.read(chars))>0) {
				sb.append(chars, 0, length);
			}
			br.close();
			is.close();
			return sb.toString();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	/**
	 * @param obj object will be generate XML string
	 * @return
	 */
	public static String genXML(Object obj){
		XStream xStream = new XStream();
		return xStream.toXML(obj);
	}
	
	public static String genJSON(Object obj){
		XStream xStream = new XStream(new JsonHierarchicalStreamDriver());
		return xStream.toXML(obj);
	}
	
	/**
	 * @param xml the XML string
	 * @return a object
	 */
	public static Object parObj(String xml, Class<?> cls) {
		XStream xStream = new XStream();
		xStream.processAnnotations(cls);
		return xStream.fromXML(xml);
	}
	

	/**
	 * @param url URL
	 * @param params parameters
	 * @return Server response String
	 */
	public static String request(String url, List<NameValuePair> params) {
		String res = null;
		try {
			HttpPost post = new HttpPost(url);
			if (params != null)
				post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
			HttpClient client = new DefaultHttpClient();
			HttpResponse response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				res = EntityUtils.toString(response.getEntity(), HTTP.UTF_8).trim();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
}















